package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.FansListContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserAttentionBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionAdd;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionCancel;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsListPage;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/20/2019 19:22
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class FansListPresenter extends BasePresenter<FansListContract.Model, FansListContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public FansListPresenter(FansListContract.Model model, FansListContract.View rootView) {
        super(model, rootView);
    }

    public void follow(ReqParamsAttentionAdd reqParamsAttentionAdd) {
        mModel.follow(AppUtil.entityToMap(reqParamsAttentionAdd)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.followSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    public void unFollow(ReqParamsAttentionCancel reqParamsAttentionCancel) {
        mModel.unFollow(AppUtil.entityToMap(reqParamsAttentionCancel)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.unFollowSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 查询关注当前用户的用户列表
     *
     * @param reqParams
     */
    public void getFansList4CurrentUserOrStore(ReqParamsListPage reqParams) {
        mModel.getFansList4CurrentUserOrStore(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFansList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    /**
     * 查询关注非当前用户或店铺的用户列表
     *
     * @param reqParams
     */
    public void getFansList4OtherUserOrStore(ReqParamsListPage reqParams) {
        mModel.getFansList4OtherUserOrStore(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFansList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
